﻿namespace EggOfPanKu.Main
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    using EggOfPanKu.Library.Objects;
    using EggOfPanKu.Main.Controls;

    /// <summary>
    /// Interaction logic for StackWindow.xaml
    /// </summary>
    public partial class StackWindow : Window
    {
        #region Fields

        public AdornerDragDrop adorner = null;
        public CardInStack elementBeingDragged;
        public ContentControl elementToShow;
        public int originalIndex;
        public Point startPoint;

        private ObservableCollection<CardInStack> _source;

        // Drag and Drop variables (temporary)
        private bool isDragInProgress;

        #endregion Fields

        #region Constructors

        public StackWindow(ObservableCollection<CardInStack> cards, Orientation orientation)
        {
            InitializeComponent();
            _source = new ObservableCollection<CardInStack>();
            _source = cards;
            CardList.ItemsSource = _source;

            if (orientation == Orientation.Horizontal)
            {
                CardList.Style = FindResource("HorizontalLB") as Style;
            }
            else
            {
                CardList.Style = FindResource("VerticalLB") as Style;
            }
        }

        #endregion Constructors

        #region Methods

        public void AddCard(CardControlStack card)
        {
        }

        public void LayoutStack()
        {
        }

        public void RemoveCard(CardControlStack card)
        {
        }

        private void CardList_DragOver(object sender, DragEventArgs e)
        {
            adorner.UpdatePosition(e.GetPosition(CardList));
        }

        private void CardList_Drop(object sender, DragEventArgs e)
        {
            if (isDragInProgress)
            {
                Point currentPos = e.GetPosition(CardList);
                ListBoxItem droppCard = GetObjectDataFromPoint(e.GetPosition(CardList)) as ListBoxItem;
                if (droppCard != null)
                {
                    int insertIndex = CardList.Items.IndexOf(droppCard.DataContext);
                    _source.RemoveAt(originalIndex);
                    _source.Insert(insertIndex, elementBeingDragged);
                }

                elementBeingDragged = null;
                isDragInProgress = false;

                RemoveAdorner();
            }
        }

        private void CardList_MouseLeave(object sender, MouseEventArgs e)
        {
        }

        private void CardList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListBoxItem selectedCard = GetObjectDataFromPoint(e.GetPosition(CardList)) as ListBoxItem;

            if (selectedCard != null)
            {
                isDragInProgress = true;
                elementBeingDragged = selectedCard.DataContext as CardInStack;
                originalIndex = CardList.Items.IndexOf(elementBeingDragged);

                //clear the adorner layer from previous adorners
                RemoveAdorner();
                //create a new adorner for the item and add it to the Adorner Layer
                adorner = new AdornerDragDrop(ContainerGrid, selectedCard);
                AdornerLayer.GetAdornerLayer(CardList).Add(adorner);

                DragDrop.DoDragDrop(CardList, selectedCard, DragDropEffects.Move);
            }
        }

        private object GetObjectDataFromPoint(Point point)
        {
            UIElement element = CardList.InputHitTest(point) as UIElement;
            if (element != null)
            {
                if (element is ListBoxItem)
                    return element;

                while(element != CardList)
                {
                    element = VisualTreeHelper.GetParent(element) as UIElement;
                    if (element is ListBoxItem)
                        return element;
                }
            }

            return null;
        }

        private void HButton_Click(object sender, RoutedEventArgs e)
        {
            CardList.Style = FindResource("HorizontalLB") as Style;
            //CardList.InvalidateVisual();
        }

        //remove the adorner from the layer
        private void RemoveAdorner()
        {
            if (adorner != null)
                //remove the adorner
                AdornerLayer.GetAdornerLayer(ContainerGrid).Remove(adorner);
        }

        private void VButton_Click(object sender, RoutedEventArgs e)
        {
            CardList.Style = FindResource("VerticalLB") as Style;
        }

        #endregion Methods
    }
}